using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TileMapClasses;

namespace AIClasses
{
	/// <summary>
	/// AI manager controls the AI move for overworld.
	/// </summary>
	public class AIManager
	{
		public static AIManager m_instance;
		private List<Tile> tempTiles;

		/// <summary>
		/// Initializes a new instance of the <see cref="AIClasses.AIManager"/> class.
		/// </summary>
		public AIManager ()
		{
			m_instance = this;	
			tempTiles = new List<Tile> ();
		}

		/// <summary>
		/// Calculates what the AI will do at the end of the turn.
		/// </summary>
		public void EndTurnMove ()
		{
			tileManager tileman = tileManager.Instance;

			int width = (int)tileman.TileMap.Width;
			int size = tileman.AmountOfTiles;

			//Loop thru enemy tiles
			for (int i = 0; i < size; i++) {
				if (tileman [i].Controlled == 2) {
					//Left
					if (InBounds (i - 1)) {
						if (i - 1 / width == i / width) {
							AddAdjTiles (i - 1);
						}
					}
					//Right
					if (InBounds (i + 1)) {
						if (i + 1 / width == i / width) {
							AddAdjTiles (i + 1);
						}
					}
					//Top-Left
					if (InBounds (i + width - 1)) {
						if (i + width - 1 / width == i + width / width) {
							AddAdjTiles (i + width - 1);
						}
					}
					//Bottom-right
					if (InBounds (i - width + 1)) {
						if (i - width + 1 / width == i - width / width) {
							AddAdjTiles (i - width + 1);
						}
					}
					//Top
					if (InBounds (i + width)) {
						AddAdjTiles (i + width);
					}
					//Bottom
					if (InBounds (i - width)) {
						AddAdjTiles (i - width);
					}
				}
			}

			//		int tileToTake = (int)(Random.value * 1000) % (tempTiles.Count);
			//		tempTiles [tileToTake].Controlled = 2;
			PickTile ();
			tempTiles.Clear ();
		}

		/// <summary>
		/// Adds the adjacent tiles around the tile at the index to the list.
		/// </summary>
		/// <param name="index">Index.</param>
		private void AddAdjTiles (int index)
		{
			Tile tempTile = tileManager.Instance [index];
			if (!TileAlreadyInList (tempTile)) {
				if (tempTile.Controlled != 2) {
					tempTiles.Add (tempTile);
				}
			}
		}

		/// <summary>
		/// Checks to see if the tile ID has already been added to the list
		/// </summary>
		/// <returns><c>true</c>, if already in the list, <c>false</c> otherwise.</returns>
		/// <param name="tempTile">tempTile.</param>
		private bool TileAlreadyInList (Tile tempTile)
		{
			foreach (Tile tileInList in tempTiles) {
				if (tileInList.TileID == tempTile.TileID) {
					return true;
				}
			}
			return false;
		}

		/// <summary>
		/// Calculation to pick a tile from the list.
		/// </summary>
		private void PickTile ()
		{
			int worth = 0;
			int ID = 0;

			foreach (Tile tempTile in tempTiles) {
				int calcedWorth = tempTile.Worth + (int)(Random.value * 10 - 2);
				if (calcedWorth > worth) {
					worth = calcedWorth;
					ID = tempTile.TileID;
				}
			}
			tileManager.Instance [ID].Controlled = 2;
		}

		/// <summary>
		/// A check to see if the given index is within the map.
		/// </summary>
		/// <returns><c>true</c>, if in bounds, <c>false</c> otherwise.</returns>
		/// <param name="index">Index.</param>
		private bool InBounds (int index)
		{
			if (index < 0) {
				return false;
			}
			if (index > (tileManager.Instance.AmountOfTiles - 2)) {
				return false;
			}
			return true;
		}

		/// <summary>
		/// Gets the instance of the <see cref="AIClasses.AIManager"/>.
		/// </summary>
		/// <value>The instance.</value>
		public static AIManager Instance {
			get {
				return m_instance;
			}
		}
	}
}
